package com.senyint.common.handle;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;

import com.senyint.common.dao.MessageDao;


public class ExecuteSqlHandle extends BaseHandle implements IHandle {
	
	private Map whereParam;
	
	private String sql;
	
	private Map outParam;
	
	private String mapKey;
	
	@Autowired
	public void setMapKey(String mapKey) {
		this.mapKey = mapKey;
	}

	@Autowired
    private MessageDao messageDao;
	
	@Autowired
	public void setWhereParam(Map whereParam) {
		this.whereParam = whereParam;
	}
	@Autowired
	public void setSql(String sql) {
		this.sql = sql;
	}
	@Autowired
	public void setOutParam(Map outParam) {
		this.outParam = outParam;
	}
	
	
	@SuppressWarnings("rawtypes")
	public Map exec(Map map){
		//传入的map  为求情的报文转换成的map
		
		//第一步     循环wheremap  把大象取出来   然后从传入的map里找到对应的值  再放回去
		//代码......
		
		//第二部   根据传入的sql  执行  然后把 whereparam  带进去  就得到要查的数据啦
		
		//第三部   把得到的数据解析了     比如  查询的是 select id from NMIS_MESSAGE  
		//通过   get(id)得到的东西  放到输出力自定义叫啥呢?       通过outparam 里面找  找到id对应的  比如 zhujian  就是要存放的key了
		
		//for (String key:outparam.keyset())
		//new 个 新map.put(outparam.get(key)=zhujian , 查询结果数据map.get(key) =查询结果对应列)
		
		
		map.put("mapKey","新map");
		//用的时候 配置文件直接可以这样写  都是从这个输入map取值的   如果是 输入进来老数据  那么 比如直接data.id  
		//如果引用处理之后的心数据  那么直接  mapkey.zhujian ....这样引入就可以了
		return outParam;
	}
    
	
	//什么list里桃map里套list  节点那种  可以把要查的参数 真正查出来  那种死的 写死在里面  得到的直接list展示
	
    private Map<String,String> xPathMap;

    public Map<String, String> getxPathMap() {
        return xPathMap;
    }
    public void setxPathMap(Map<String, String> xPathMap) {
        this.xPathMap = xPathMap;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public Object Invoke(Object... obj) throws Exception {
        
        Map data = null;
        HttpServletRequest request = null;
        HttpServletResponse response = null;
        
        for (Object object : obj) {
            if(object instanceof Map){
                data = (HashMap)object;
            }
            if(object instanceof HttpServletRequest){
                request = (HttpServletRequest)object;
            }
            if(object instanceof HttpServletResponse){
                response = (HttpServletResponse)object;
            }
        }
        
        if(data == null || request == null || response == null){
            logger.debug("LoadCommandHandle参数错误"); 
        }
        
        Document doc = (Document)data.get(getInParamList().get(0));
        
        if(doc == null){
            logger.error("LoadCommandHandle:Document为空");
            return null;
        }
        
        logger.debug(doc.asXML());
        Element root = doc.getRootElement();
        
        Element element;
        Map docData = new HashMap();
        for (String key : this.xPathMap.keySet()) {
            String[] value = this.xPathMap.get(key).split(":");
            if(value[1].equals("text")){
                element = (Element) root.selectSingleNode(value[0]);
                docData.put(key, element.getText());
            }else if(value[1].equals("map")){
                element = (Element) root.selectSingleNode(value[0]);
                List<?> datas = element.elements();
                Map d = new HashMap();
                for(int index=0;index<datas.size();index++){
                    Element el = (Element)datas.get(index);
                    d.put(el.getName(), el.getTextTrim());
                }
                docData.put(key, d);
            }
        }
        
        data.put(this.getOutParamList().get(0), docData);
        
        return data;
    }

}
